package com.util.encrypt;


import org.apache.log4j.Logger;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author victor
 */
public class Des {
    private static final String Algorithm = "DES";
    /*创建Log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(Des.class);

    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
        try {

            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            LOGGER.error("Des类方法encryptMode错误: NoSuchAlgorithmException", e1);
        } catch (javax.crypto.NoSuchPaddingException e2) {
            LOGGER.error("Des类方法encryptMode错误: NoSuchPaddingException", e2);
        } catch (java.lang.Exception e3) {
            LOGGER.error("Des类方法encryptMode错误: java.lang.Exception", e3);
        }
        return null;
    }


    public static byte[] decryptMode(byte[] keybyte, byte[] src) {
        try {
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            LOGGER.error("Des类方法decryptMode错误: NoSuchAlgorithmException", e1);
        } catch (javax.crypto.NoSuchPaddingException e2) {
            LOGGER.error("Des类方法decryptMode错误: NoSuchPaddingException", e2);
        } catch (java.lang.Exception e3) {
            LOGGER.error("Des类方法decryptMode错误: java.lang.Exception", e3);
        }
        return null;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) hs = hs + "0" + stmp;
            else hs = hs + stmp;
            if (n < b.length - 1) hs = hs + "";
        }
        return hs.toUpperCase();
    }

    public static byte[] hex2byte(String hex) throws IllegalArgumentException {
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException();
        }
        char[] arr = hex.toCharArray();
        byte[] b = new byte[hex.length() / 2];
        for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
            String swap = "" + arr[i++] + arr[i];
            int byteint = Integer.parseInt(swap, 16) & 0xFF;
            b[j] = new Integer(byteint).byteValue();
        }
        return b;
    }

    private static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    public static String Encrypt(String str, byte[] key) {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] encrypt = encryptMode(key, str.getBytes());
        return byte2hex(encrypt);
    }

    public static byte[] EncryptRetByte(byte[] src, byte[] key) {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] encrypt = encryptMode(key, src);
        return encrypt;
    }

    public static String Decrypt(String str, byte[] key) {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        byte[] decrypt = decryptMode(key, hex2byte(str));
        return new String(decrypt);
    }

    /**
     * 此类主要用于jdbc.properties中jdbc的url,username,password的加密密文的获取，非常用类
     */
    public static void main(String arg[]) {

        // jdbc-url: jdbc:mysql://localhost:3306/kuibusoftware
        // Aliyun-RDS(internal): jdbc:mysql://rm-uf6po11tq87945gav.mysql.rds.aliyuncs.com:3306/kuibusoftware
        // Aliyun-RDS(external): jdbc:mysql://sub1463935599259-rdse5ureq44zwrp74g4y.mysql.rds.aliyuncs.com:3306/kuibusoftware

        String str = "jdbc:mysql://rm-uf6po11tq87945gav.mysql.rds.aliyuncs.com:3306/kuibusoftware";
        String strKey = "1016521552150002";
        String s3 = Encrypt(str, hex2byte(strKey));
        String s4 = Decrypt(s3, hex2byte(strKey));
        System.out.println(s3);
        System.out.println(s4);
    }
}

